package com.mercury.emqx.manager;//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//


import com.mercury.emqx.client.MqttClient;
import com.mercury.emqx.client.req.MqttPublishReq;
import com.mercury.emqx.client.req.MqttSubscribeReq;
import com.mercury.emqx.client.resp.MqttResp;
import com.mercury.emqx.model.PayloadMessage;
import com.mercury.emqx.properties.MqttProperties;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author mercury
 */
@Component
public class MqttManager {

    @Resource
    private MqttProperties mqttProperties;

    @Resource
    private MqttClient mqttClient;

    public MqttManager() {
    }

    public MqttResp<Object> sendMsg(String topic, String message) {
        PayloadMessage payloadMessage = new PayloadMessage();
        payloadMessage.setClientId(this.mqttProperties.getClientId());
        payloadMessage.setTopic(topic);
        payloadMessage.setPayload(message);
        String url = this.mqttProperties.getMqttHttpUrl() + MqttClient.PUBLISH;
        return this.mqttClient.publish(url, new MqttPublishReq(payloadMessage));
    }

    public MqttResp<Object> sendMsg(PayloadMessage message) {
        String url = this.mqttProperties.getMqttHttpUrl() + MqttClient.PUBLISH;
        return this.mqttClient.publish(url, new MqttPublishReq(message));
    }


    public MqttResp<Object> subscribe(String topic) {
        PayloadMessage message = new PayloadMessage();
        message.setClientId(this.mqttProperties.getClientId());
        message.setQos(2);
        message.setTopic("");
        String url = this.mqttProperties.getMqttHttpUrl() + MqttClient.SUBSCRIBE;
        return this.mqttClient.subscribe(url, new MqttSubscribeReq(message));
    }

}
